﻿using NodeEditor;

namespace ETHotfix
{
    [NodeEvent(typeof(ReplaceSpriteNode))]
    public class ReplaceSpriteNodeHandle : NodeHandle<ReplaceSpriteNode>
    {
        const string spriteStr = "sprite";
        protected override void OnHandle(RuntimeTree runtimeTree, RuntimeNode runtimeNode, ReplaceSpriteNode data, Unit unit)
        {
            var rc = unit.target.GetComponent<ReferenceCollector>();
            if (rc == null)
                return;

            var go = rc.Get<UnityEngine.GameObject>(data.gameObjectName);
            if (go == null)
                return;

            var renderer = go.GetComponent<UnityEngine.SpriteRenderer>();
            if (renderer == null)
                return;

            data.sprite = runtimeNode.GetInputValue(spriteStr, data.sprite);
            renderer.sprite = data.sprite;
        }
    }
}